package com.walter.model.vo;

import cn.hutool.core.bean.BeanUtil;
import com.walter.Application;
import com.walter.entity.Role;
import com.walter.entity.User;
import com.walter.mapper.RoleMapper;
import com.walter.util.SpringUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author ：ch
 * @date ：Created in 2021/5/25
 * @description：
 * @modified By：
 * @version: 1.0
 */
@Data
@Accessors(chain = true)
public class UserInfoVO {
    @ApiModelProperty("主键，添加不能传，修改必须传")
    private Integer id;

    @ApiModelProperty("用户名")
    @NotNull(message = "用户名不能为空")
    @Length(min = 1,message = "用户名不能为空串")
    private String userName;

    @ApiModelProperty("昵称")
    @NotNull(message = "昵称不能为空")
    @Length(min = 1,message = "昵称不能为空串")
    private String nickName;

    @ApiModelProperty("电子邮箱")
    private String email;

    @ApiModelProperty("uploadPicture接口返回的url")
    private String avatar;

    @ApiModelProperty("性别")
    @NotNull(message = "性别不能为空")
    private String gender;

    @ApiModelProperty(hidden = true)
    private String genderName;

    @ApiModelProperty("腾讯QQ")
    private String qq;

    private String isEnabled;

    @ApiModelProperty("角色列表,查询用到")
    private List<Role> roleList;

    @ApiModelProperty("角色id列表,用于添加修改用户")
    @Size(min = 1,message = "用户至少需要绑定一个角色")
    private List<Integer> roleIds;

    public UserInfoVO(){

    }

    public UserInfoVO(User user){
        if (null != user){
            BeanUtil.copyProperties(user,this);
            this.setGenderName(Application.secondLevelDictMap.getStr(this.getGender()));
            RoleMapper roleMapper = SpringUtil.getBean(RoleMapper.class);
            List<Role> roles = roleMapper.findByUserId(user.getId());
            this.setRoleList(roles);
        }
    }
}
